<?php
namespace Home\Controller;
use Think\Controller;
/**
**@desc 模拟api生成
*/

class ModelapiController extends CommonController {
	private $auth_status = 0;//默认：未认证
    private $user_email = null;//用户邮箱
    private $uid = null;
    private $auth_type = 1;
    private $user_name = '';

    public function __construct(){
        //是否服务商用户，且登录状态
        parent::__construct();

		//判断用户是否服务商用户
        if($this->role==1){
            redirect('/Index/index');
        }
        
        $this->getAuthStatus();
        $this->auth_status = session('37degree_auth_status');
        
        $this->user_email = session('user_email');
        $this->uid = session('uid');
        $this->auth_type = session('auth_type');
        $this->user_name = session('user_name');

        $this->assign('user_email',$this->user_email);
        if(empty($this->uid)) redirect('/member/login.html?role=company');
    }
    
	//api生成
	public function add(){
        if(IS_POST){
            echo "add ajax";die;
        }else{
            //获取数据源
            $ModelApiModel = D('ModelApi');
            $import_data = $ModelApiModel->getImportList($this->uid);

            $this->assign('import_data',$import_data);
            $this->display();    
        }
        
	}

	//api编辑
	public function edit(){
        if(IS_POST){
            echo "ajax";die;
        }else{
            //获取数据源
            $ModelApiModel = D('ModelApi');
            $import_data = $ModelApiModel->getImportList($this->uid);

            $this->assign('import_data',$import_data);
            $this->display();    
        }
        
	}

	//api发布
	public function publish(){

	}

	//api删除
	public function del(){

	}

    //调试
    public function tool(){

    }

	//api管理列表页面
    public function index(){
        //获取该用户导入数据
        $ModelApiModel = D('ModelApi');
        $exists_import = $ModelApiModel->isexistsImport($this->uid);

        $this->assign('exists_import',$exists_import);
        $this->display();
    }

    //获取列表
    public function ajaxList(){
        $page = I('post.page',1,'intval');
        $pagesize = I('post.pagesize',20,'intval');
        $status = I('post.status',-1,'intval');

        $ApiModel = D('ModelApi');
        $list = $ApiModel->getList($status,$page,$pagesize);

        $data['page'] = $page;
        $data['list'] = $list['list'];
        $data['totalpage'] = ceil($list['count']/$pagesize);

        $this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$data) );

    }

    //ajax获取 数据源的具体详情
    public function ajaxImportInfo(){
        $id = I('post.id',null,'intval');
        $type = I('post.type',1,'intval');

        if(empty($id)) $this->ajaxReturn( array('status'=>1002,'msg'=>'缺少参数','data'=>null) ); 
        if(empty($this->uid)) $this->ajaxReturn( array('status'=>1005,'msg'=>'请重新登录','data'=>null) ); 
        
        $ModelApiModel = D('ModelApi');
        $info = $ModelApiModel->getImportInfo($id,$type);
        $this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$info) );
    }

}